<template>
  <div>
    <el-tabs v-model="activeName" style="width: 581px">
      <el-tab-pane label="基本设置"  name="first">
        <el-form ref="form"  :rules="rules"  :model="step" label-width="150px">
          <el-form-item label="步骤名称" prop="name">
            <el-input v-model="step.name" placeholder="请输入步骤名称"></el-input>
          </el-form-item >
          <el-form-item label="文件路径">
            <el-select :disabled="step.isInFields=='Y'" v-model="filePath" >
              <el-option value="${fileFolder}">默认路径</el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="文件名称">
            <el-input v-model="fileName" :disabled="step.isInFields=='Y'"></el-input>
          </el-form-item>
          <el-form-item label="从字段中获取文件名?">
            <el-checkbox true-label="Y" false-label="N" v-model="step.isInFields"></el-checkbox>
          </el-form-item>
          <el-form-item label="文件名字段名">
            <el-select :disabled="step.isInFields=='N'" v-model="step.dynamicFilenameField" >
              <el-option v-for="item in step.availableFields" :value="item.name"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-tab-pane>

      <el-tab-pane label="输出设置" name="second">
        <el-form ref="form2" label-width="150px">
          <el-row type="flex" justify="end" style="margin-bottom: 10px">
            <el-col :span="5">
              <el-button icon="plus" @click="addOutPut">添加输出</el-button>
            </el-col>
          </el-row>
          <el-table :data="step.fields" stripe border :max-height="250">
            <el-table-column type="index" :width="59"></el-table-column>
            <el-table-column label="名称" :width="230">
              <template scope="scope">
                <div>
                  <el-input v-model="scope.row.name"></el-input>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="元素" :width="90">
              <template scope="scope">
                <div>{{scope.row.element_type}}</div>
              </template>
            </el-table-column>
            <el-table-column label="空格处理" :width="120">
              <template scope="scope">
                <div v-if="scope.row.element_type =='size'">{{scope.row.trim_type}}</div>
                <div v-else>
                  <el-select v-model="scope.row.trim_type">
                    <el-option value="none"></el-option>
                    <el-option value="both"></el-option>
                  </el-select>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="控制" :width="80">
              <template scope="scope">
                    <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row,step.fields)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form>
      </el-tab-pane>

    </el-tabs>
  </div>
</template>

<script>
  import {Steps, Fields} from "../../model/DataModel";
  import StepCommon from "./mixins/StepMixin";

  export default{
    mixins: [StepCommon],
    mounted(){
        let fileName = this.step.fileName;
        if(fileName!=""){
            let index = fileName.indexOf("}");
            this.filePath = fileName.substring(0,index+1);
            this.fileName = fileName.substring(index+1);
        }
    },
    watch:{
      'filePath':function(newValue){
        this.setFileName();
      },
      'fileName':function(newValue){
        this.setFileName();
      }
    },
    data(){
      return {
        activeName: "first",
        step: Steps.LoadFileInput(),
        filePath:"",
        fileName:""
      }
    },
    methods: {
      setFileName(){
        this.step.fileName = this.filePath + this.fileName;
      },
      addOutPut(){
        if(this.step.fields.length > 0){
          this.confirm('该操作将重置输出列表，是否继续？',this.doAddOutPut);
        }else{
          this.doAddOutPut();
        }
      },
      doAddOutPut(){
        this.step.fields = [];
        this.step.fields.push(Fields.LoadFileInput({name:'file_content',element_type:'content',type:'String',trim_type:'both'}));
        this.step.fields.push(Fields.LoadFileInput({name:'file_size',element_type:'size',type:'Integer',trim_type:'none'}));
      },
      updateFields(){
        let fields=this.step.fields;
        if(fields.length>0) {
          this.step.outputFields = fields.map((el)=>{
              return el.name
          }).filter(el => el != "");
        }
      }
    }
  }
</script>
